package com.zhejiangzhengyuan.municipal_common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.File;
import com.zhejiangzhengyuan.municipal_common.entity.RoadFileMapping;
import com.zhejiangzhengyuan.municipal_common.entity.RoadGis;
import com.zhejiangzhengyuan.municipal_common.entity.dto.FileDto;
import com.zhejiangzhengyuan.municipal_common.entity.vo.RoadVo;
import com.zhejiangzhengyuan.municipal_common.mapper.FileMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.RoadFileMappingMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.RoadGisMapper;
import com.zhejiangzhengyuan.municipal_common.service.RoadGisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhejiangzhengyuan.municipal_common.utils.FileDuplicateCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.zhejiangzhengyuan.municipal_common.utils.DateFormatUtil.timeFormat;
import static com.zhejiangzhengyuan.municipal_common.utils.StaticConstants.*;


/**
 * <p>
 * GIS数据库中道路表（一路一档建设） 服务实现类
 * </p>
 *
 * @author linju
 * @since 2019-11-22
 */
@Service
@Slf4j
@Transactional
public class RoadGisServiceImpl extends ServiceImpl<RoadGisMapper, RoadGis> implements RoadGisService {
    @Autowired
    private RoadGisMapper roadGisMapper;
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private FileDuplicateCode fileDuplicateCode;
    @Autowired
    private RoadFileMappingMapper roadFileMappingMapper;



    public IPage<RoadGis> page(Long pageNo, Long pageSize) {
        IPage<RoadGis> page = new Page<>(pageNo, pageSize);
        QueryWrapper<RoadGis> wrapper = new QueryWrapper<>();
        QueryWrapper<RoadGis> id = wrapper.orderByAsc("id");
        IPage<RoadGis> roadGisIPage = roadGisMapper.selectPage(page, id);
        List<RoadGis> records = roadGisIPage.getRecords();
        for (RoadGis record : records) {
            Map<String, Double> map = roadGisMapper.findByIdPoint(record.getId());
            record.setStartPoint(map.get("start_x") + "," + map.get("start_y"));
            record.setEndPoint(map.get("end_x") + "," + map.get("end_y"));
        }
        return roadGisIPage;
    }

    //修改方法
    @Transactional(rollbackFor = Exception.class)
    public int updateRoad(RoadGis roadGis) {
        if (roadGisMapper.selectById(roadGis.getId()) == null) {
            return 0;
        }
        //设置的更新时间
        roadGis.setUpdateTime(timeFormat(System.currentTimeMillis()));
        String startPoint = roadGis.getStartPoint();
        String endPoint = roadGis.getEndPoint();
        roadGis.setStartPoint(null);
        roadGis.setEndPoint(null);
        if (roadGisMapper.updateById(roadGis) > 0) {
            if (Strings.isBlank(startPoint)) {
                String[] split = startPoint.split(",");
                roadGisMapper.updateStartPoint(roadGis.getId(), split[0], split[1]);
            }

            if (Strings.isBlank(endPoint)) {
                String[] split = endPoint.split(",");
                roadGisMapper.updateEndPoint(roadGis.getId(), split[0], split[1]);
            }
            return 1;
        }
        return 0;
    }

    //添加方法
    @Transactional(rollbackFor = Exception.class)
    public int saveRoad(RoadGis roadGis) {
        //longitude,latitude
        String[] startsplit = roadGis.getStartPoint().split(",");
        String[] endsplit = roadGis.getEndPoint().split(",");
        //桥梁的坐标 后续修改
        roadGis.setStartPoint(null);
        roadGis.setEndPoint(null);
        roadGis.setCreateTime(timeFormat(System.currentTimeMillis()));
        //桥梁的更新时间
        roadGis.setUpdateTime(timeFormat(System.currentTimeMillis()));
        if (roadGisMapper.insert(roadGis) > 0) {
            //插入成功后修改geometry
            return roadGisMapper.updatePoint(roadGis.getId(), startsplit[0], startsplit[1], endsplit[0], endsplit[1]);
        }
        return 0;
    }

    @Override
    public RoadGis findPointByRoad(String startPoint, String endPoint) {
        if (Objects.isNull(startPoint)) {
            return null;
        }
        if (Objects.isNull(endPoint)) {
            return null;
        }
        String[] startSplit = startPoint.split(",");
        String[] endSplit = endPoint.split(",");
        return roadGisMapper.findPointByRoad(startSplit[0], startSplit[1], endSplit[0], endSplit[1]);
    }

    @Override
    public int updateRoadFile(FileDto dto) {
        Long id = dto.getId();
        MultipartFile multipartFile = dto.getFile();
        if (multipartFile == null || id == null) {
            return 0;
        }
        if (Objects.isNull(roadGisMapper.selectById(id))) {
            return 0;
        }
        File file = new File();
        if (fileDuplicateCode.insertFileDuplicateCode(file, multipartFile, WAY_MODULE) > 0) {
            RoadFileMapping roadFileMapping =
                    new RoadFileMapping();
            roadFileMapping.setCreateTime(timeFormat(System.currentTimeMillis()));
            roadFileMapping.setUpdateTime(timeFormat(System.currentTimeMillis()));
            roadFileMapping.setRoadId(id);
            roadFileMapping.setFileId(file.getId());
            roadFileMappingMapper.insert(roadFileMapping);
        }
        return 1;

    }

    @Override
    public IPage<RoadVo> selectAllRoad(Page<RoadVo> roadVoPage) {
        Page<RoadVo> voPage = roadGisMapper.getVoPage(roadVoPage);
        List<RoadVo> records = voPage.getRecords();
        for (RoadVo roadVo : records) {
            List<String> strings = fileMapper.selectByRid(roadVo.getId());
            if (strings!=null&&strings.size()!=0) {
                roadVo.setLink(strings);
            }
        }
        return voPage;
    }


}
